﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TalksLog.Domain.Account.Common;
using TalksLog.Domain.Account.Entities;
using TalksLog.Domain.Account.Repositores;
using TalksLog.Domain.ValueObjects;

namespace TalksLog.Domain.Account.Services
{
    /// <summary>
    /// 帐号相关
    /// </summary>
    public class AccountService
    {
        private IAccountRepository _repository;

        public AccountService(IAccountRepository repository)
        {
            _repository = repository;
        }

        public bool DoesEmailExist(string email)
        {
            return _repository.DoesEmailExist(email);
        }

        public bool DoesNameExist(string name)
        {
            return _repository.DoesNameExist(name);
        }

        public Entities.Account CreateNewAccount(string name, 
            string password, 
            Gender gender, 
            string email,
            DateTime? birthday,
            Address address,
            Contacts contacts,
            int id)
        {
            Entities.Account account = Entities.Account.Create(name,
                password,
                gender,
                email,
                birthday,
                address,
                contacts);
            account.Id = id;

            return account;
        }

        public Entities.Account FindByEmailAndPassword(string email, string password)
        {
            var account = _repository.FindByEmail(email);
            if (account == null)
            {
                return null;
            }

            string passwordWithSalt = PasswordConverter.GetPasswordWithSalt(email, password, account.Salt);
            if (account.Password != passwordWithSalt)
            {
                return null;
            }

            return account;
        }
    }
}
